﻿using System.IO;
using System.Net;
using System.Text;

namespace ThinkAway.Net.Http
{
    /// <summary>
    /// HttpClient
    /// </summary>
    public class HttpClient
    {
        /// <summary>
        /// 默认HTTP头：UserAgent
        /// </summary>
        public string DefaultUserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";

        /// <summary>
        /// UserAgent
        /// </summary>
        public string UserAgent
        {
            get { return DefaultUserAgent; }
            set { DefaultUserAgent = value; }
        }

        /// <summary>
        /// 默认HTTP头：Accept
        /// </summary>
        public string DefaultAccept = "text/html, application/xhtml+xml, */*";

        public string Accept
        {
            get { return DefaultAccept; }
            set { DefaultAccept = value; }
        }

        /// <summary>
        /// 默认HTTP头：ContentType
        /// </summary>
        public static string DefaultContentType = "application/x-www-form-urlencoded";

        /// <summary>
        /// ContentType
        /// </summary>
        public string ContentType
        {
            get { return DefaultContentType; }
            set { DefaultContentType = value; }
        }

        /// <summary>
        /// 默认编码
        /// </summary>
        public static Encoding DefaultEncoding = Encoding.UTF8;

        /// <summary>
        /// 编码
        /// </summary>
        public Encoding Encoding
        {
            get { return DefaultEncoding; }
            set { DefaultEncoding = value; }
        }

        /// <summary>
        /// 空Cookie
        /// </summary>
        public static CookieContainer DefaultCookie = new CookieContainer(1000, 1000, 100000);

        /// <summary>
        /// Cookie
        /// </summary>
        public static CookieContainer Cookie
        {
            get { return DefaultCookie; }
            set { DefaultCookie = value; }
        }

        /// <summary>
        /// 读取Cookies
        /// </summary>
        /// <returns>成功与否</returns>
        internal void SetCookies(CookieContainer cookieContainer)
        {
            Cookie = cookieContainer;
        }

        /// <summary>
        /// 设置代理
        /// </summary>
        /// <param name="host">主机名</param>
        /// <param name="port">端口</param>
        public void SetProxy(string host, int port)
        {
            System.Net.WebRequest.DefaultWebProxy = new WebProxy(host, port);
        }

        /// <summary>
        /// 使用默认代理
        /// </summary>
        public void ResetProxy()
        {
            System.Net.WebRequest.DefaultWebProxy = System.Net.WebRequest.GetSystemWebProxy();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="accept"></param>
        /// <param name="referer"></param>
        /// <param name="contentType"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        protected virtual string RequestData(string url, string method, string accept, string referer,string contentType, byte[] data)
        {
            string result = null;
            HttpWebRequest request = System.Net.WebRequest.Create(url) as HttpWebRequest;
            if (request != null)
            {
                request.Accept = accept;
                request.Method = method;
                request.KeepAlive = true;
                request.AllowAutoRedirect = true;
                request.UserAgent = UserAgent;
                request.ContentType = contentType;
                request.CookieContainer = Cookie;
                if (!string.IsNullOrEmpty(referer))
                {
                    request.Referer = referer;
                }
                if (data != null && data.Length > 0)
                {
                    request.ContentLength = data.Length;

                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(data, 0, data.Length);
                    }
                }

                using (HttpWebResponse responce = request.GetResponse() as HttpWebResponse)
                using (StreamReader sr = new StreamReader(responce.GetResponseStream(), Encoding))
                {
                    result = sr.ReadToEnd();
                }
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Post(string url, byte[] data)
        {
            return this.Post(url, Accept, "", ContentType, data);
        }

        /// <summary>
        /// 用Post方法发送请求
        /// </summary>
        /// <param name="postUri">请求的地址</param>
        /// <param name="accept">Accept头</param>
        /// <param name="referer">Referer头</param>
        /// <param name="contentType">ContentType头</param>
        /// <param name="content">请求正文</param>
        /// <returns>
        /// 响应正文
        /// </returns>
        public string Post(string postUri, string accept, string referer, string contentType, byte[] content)
        {
            return RequestData(postUri, HttpMethod.POST, accept, referer, contentType, content);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string Get(string url)
        {
            return this.Get(url, Accept, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="urlBuilder"></param>
        /// <returns></returns>
        public string Get(UrlBuilder urlBuilder)
        {
            return this.Get(urlBuilder.ToString(), Accept, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="accept"></param>
        /// <param name="referer"></param>
        /// <returns></returns>
        public string Get(string url, string accept, string referer)
        {
            return RequestData(url, HttpMethod.GET, accept, referer, ContentType, null);
        }
    }

}
